<template>
  <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <div class="app-container">
      <el-button @click="handleAddGoodsGx" icon="el-icon-plus" plain size="mini" type="primary">新增产品工序
      </el-button>
      <el-popconfirm :title="`确认删除数据项？`" @confirm="handleDelete({})" style="margin-left: 10px;">
        <!--<el-button slot="reference" type="danger" plain icon="el-icon-delete" :disabled="multiple" size="mini"
                   style="margin-left:10px;" >批量删除产品工序
        </el-button>-->
      </el-popconfirm>
      <el-table border v-loading="loading" :data="dataList" ref="table" show-summary :summary-method="getSummaries"
        :max-height="tableMaxHeight" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="xuhao" />
        <el-table-column label="生产单号" align="center" prop="produceNo" />
        <el-table-column label="订单编号" align="center" prop="projectNo" />
        <el-table-column sortable label="工程名称" align="center" prop="gongchengName" />
        <el-table-column label="产品编码" align="center" prop="goodsCode" />
        <el-table-column label="产品名称" align="center" prop="goodsName" />
        <el-table-column label="产品型号" align="center" prop="goodModel" />
        <el-table-column label="工序编码" align="center" prop="code" />
        <el-table-column label="工序名称" align="center" prop="name" />
        <el-table-column sortable align="center" label="是否最后一道工序" prop="islast">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.yes_no" :value="scope.row.islast" />
          </template>
        </el-table-column>
        <el-table-column label="车间" align="center" prop="deptName" />
        <el-table-column label="标准工时" align="center" prop="workHour" />
        <el-table-column label="工序产品名称" align="center" prop="gongxuName" />
        <el-table-column label="工序产品型号" align="center" prop="gongxuModel" />
        <el-table-column label="图纸号" align="center" prop="shuoming" />
        <!--<el-table-column label="工艺编码" align="center" prop="gongyiCode"/>-->
        <el-table-column label="工艺名称" align="center" prop="gongyiName" />
        <el-table-column label="总数量" align="center" prop="counts" />
        <el-table-column label="已分配数量" align="center" prop="fenpeiCount" />
        <el-table-column label="剩余分配数量" align="center" prop="remainCount" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button slot="reference" type="text" plain icon="el-icon-edit" size="mini"
              @click="piliangPaigong(scope.row)" v-hasPermi="['develop:PaichanTask:piliangBnt']">计件派工</el-button>
            <el-button slot="reference" type="text" plain icon="el-icon-edit" size="mini"
              @click="piliangPaigongJishi(scope.row)" v-hasPermi="['develop:PaichanTask:piliangBnt']">计时派工</el-button>
            <el-popconfirm :title="`确认删除数据项？`" @confirm="handleDelete(scope.row)" style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete">删除产品工序</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <goods-gongxu-add-form ref="goodsGongxuForm" @ok="getList()"></goods-gongxu-add-form>
      <piliangPaigong-form @ok="getList" ref="piliangPaigongForm"></piliangPaigong-form>
      <piliangPaigongJishi-form @ok="getList" ref="piliangPaigongJishiForm"></piliangPaigongJishi-form>
      <!--<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>-->
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { deleteAction, getAction, accAdd } from '@/api/manage'
import GoodsGongxuAddForm from '@/views/develop/goodsSaledoGongxu/module/GoodsGongxuAddForm'
import PiliangPaigongForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/PiliangPaigongForm";
import PiliangPaigongJishiForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/PiliangPaigongJishiForm";

export default {
  name: "GongxuProdForm",
  dicts: ['yes_no'],
  components: {
    GoodsGongxuAddForm,
    PiliangPaigongForm,
    PiliangPaigongJishiForm,
  },
  data() {
    return {
      activeName: "first",
      // 弹出层标题
      title: "产品工序查看1",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      saleId: null,
      goodsCount: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单产品工艺工序关联表格数据
      dataList: [],
      selectRows: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10000,
        xuhao: null,
        code: null,
        name: null,
        shuoming: null,
        file: null,
        photo: null,
        status: null,
        saleId: null,
        goodsId: null,
        gongyiId: null,
        gongxuId: null,
        gongyiCode: null,
        gongyiName: null,
        /*orderByColumn: "xuhao",
        isAsc: "asc",*/
      },
      url: {
        list: '/goodsSaledoGongxu/goodsSaledoGongxu/listSalePro',
        remove: '/goodsSaledoGongxu/goodsSaledoGongxu/remove/',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },

  methods: {
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "counts" || column.property === "fenpeiCount" || column.property === "remainCount") {
          sums[index] = values.reduce((prev, curr) => {

            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
    piliangPaigong(row) {
      this.selectRows = [];
      this.selectRows.push(row);
      this.$refs.piliangPaigongForm.piliangFenpeiGxpShow(this.selectRows);
    },
    piliangPaigongJishi(row) {
      this.selectRows = [];
      this.selectRows.push(row);
      this.$refs.piliangPaigongJishiForm.piliangFenpeiGxpShow(this.selectRows);
    },
    handleAddGoodsGx() {
      this.$refs.goodsGongxuForm.handleAddGoodsGongxu(this.saleId, this.goodsCount);
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    getList() {
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查询订单产品工艺工序关联列表 */
    gongxuShow(row) {
      this.loading = true;
      this.open = true;
      this.queryParams.saleId = row.id;
      this.saleId = row.id;
      this.goodsCount = row.count;
      this.getList();
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
  }
};
</script>
